package ace.tool.generate.code.impl.resolver.scheme.resolver.core;

import ace.tool.generate.code.impl.manager.common.EntityModelManager;
import ace.tool.generate.code.impl.resolver.scheme.model.bo.ApiProject;
import ace.tool.generate.code.impl.resolver.scheme.model.bo.api.ApiClassInfo;
import ace.tool.generate.code.impl.resolver.scheme.model.bo.api.DtoClassInfo;
import ace.tool.generate.code.impl.resolver.scheme.model.bo.core.ApiImplClassInfo;
import ace.tool.generate.code.impl.resolver.scheme.model.bo.core.EntityClassInfo;
import ace.tool.generate.code.impl.resolver.scheme.model.bo.core.ManagerClassInfo;
import ace.tool.generate.code.impl.resolver.scheme.model.dto.ClassFileInfo;
import ace.tool.generate.code.impl.resolver.scheme.model.dto.CommonInfo;
import ace.tool.generate.code.impl.resolver.scheme.model.dto.ProjectInfo;
import ace.tool.generate.code.impl.resolver.scheme.resolver.ClassFileInfoResolver;
import ace.tool.generate.code.impl.service.model.input.GenerateInput;
import java.util.List;

/**
 * @author caspar
 * @date 2023/11/17 10:34
 */

public class ApiImplClassInfoResolver {

  private final GenerateInput input;
  private final EntityClassInfo entityClassInfo;
  private final ProjectInfo coreProjectInfo;
  private final CommonInfo commonInfo;
  private final ApiProject apiProject;
  private final List<ManagerClassInfo> managerClassInfos;
  private final EntityModelManager entityModelManager = new EntityModelManager();

  public ApiImplClassInfoResolver(GenerateInput input, EntityClassInfo entityClassInfo, ProjectInfo coreProjectInfo, CommonInfo commonInfo, ApiProject apiProject,
      List<ManagerClassInfo> managerClassInfos) {
    this.input = input;
    this.entityClassInfo = entityClassInfo;
    this.coreProjectInfo = coreProjectInfo;
    this.commonInfo = commonInfo;
    this.apiProject = apiProject;
    this.managerClassInfos = managerClassInfos;
  }


  public ApiImplClassInfo resolve() {

    ClassFileInfo classFileInfo = ClassFileInfoResolver.builder()
        .className(entityClassInfo.getModel().getClassName())
        .classNameSuffix(input.getApiImplClassNameSuffix())
        .classPackagePrefix(input.getApiImplClassPackagePrefix())
        .rootPackageFullName(commonInfo.getRootPackageFullName())
        .sourceCodeRootDir(coreProjectInfo.getSourceCodeRootDir())
        .build()
        .resolve();

    String entityModelId = entityClassInfo.getEntityModelId();

    ApiClassInfo apiClassInfo = this.entityModelManager.getApiClassInfoByEntityModelId(apiProject.getApiClasses(), entityModelId);
    DtoClassInfo dtoClassInfo = this.entityModelManager.getDtoClassInfoByEntityModelId(apiProject.getDtoClasses(), entityModelId);
    ManagerClassInfo managerClassInfo = this.entityModelManager.getManagerClassInfoByEntityModelId(managerClassInfos, entityModelId);
    return ApiImplClassInfo.builder()
        .entityModelId(entityClassInfo.getEntityModelId())
        .info(classFileInfo)
        .apiClassInfo(apiClassInfo)
        .dtoClassInfo(dtoClassInfo)
        .managerClassInfo(managerClassInfo)
        .entityClassInfo(entityClassInfo)
        .build();
  }
}
